package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.domain.PageResult;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpecificationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("specification")
public class SpecificationController {

    @DubboReference
    private SpecificationService specificationService;

    @RequestMapping("specificationShow")
    public String specificationShow(){
        return "admin/specification";
    }

    @RequestMapping("specificationList")
    @ResponseBody
    public AjaxResult getInfo(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize,TbSpecification specification){
        PageResult<TbSpecification> list = specificationService.getInfo(pageNum,pageSize,specification);
        return AjaxResult.ok().setData(list);
    }

    @RequestMapping("toAdd")
    @ResponseBody
    public AjaxResult toAdd(TbSpecification tbSpecification,String listJson){
        List<TbSpecificationOption> options = JSONObject.parseArray(listJson, TbSpecificationOption.class);
        specificationService.toAdd(tbSpecification,options);
        return AjaxResult.ok();
    }

    @RequestMapping("update")
    @ResponseBody
    public AjaxResult update(Long id){
        List<TbSpecification> list = specificationService.update(id);
        return AjaxResult.ok().setData(list);
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public AjaxResult updateInfo(TbSpecification specification,@RequestParam("listJson")String listJson){
        List<TbSpecificationOption> options = JSONObject.parseArray(listJson, TbSpecificationOption.class);
        specificationService.updateInfo(specification,options);
        return AjaxResult.ok();
    }

    @RequestMapping("deleteInfo")
    @ResponseBody
    public AjaxResult deleteInfo(@RequestParam("ids[]")Long[] ids){
        specificationService.deleteInfo(ids);
        return AjaxResult.ok();
    }
}
